package com.openjaw.lsy.agent.core.plugin;

import com.openjaw.lsy.agent.core.config.Config;
import com.openjaw.lsy.agent.core.logging.api.ILog;
import com.openjaw.lsy.agent.core.logging.api.LogManager;
import com.openjaw.lsy.agent.core.plugin.enums.EnumAgentConfig;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

/**
 * Find the enhanceClassName in the configuration file and get the className to be enhanced.
 *
 * @author binbin.zhang
 */
public class EnhanceClassNameResolver {
    private static final ILog logger = LogManager.getLogger(EnhanceClassNameResolver.class);

    public List<String> getEnhanceClassNames() {
        List<String> list = new ArrayList<>();
        Properties prop = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(Config.Agent.JAR_PATH + "/config/AgentConfig.properties"));
            prop.load(in);     ///加载属性列表
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                String key = it.next();
                if (key.equals(EnumAgentConfig.ENHANCECLASSNAME.getValue())) {
                    list = Arrays.asList(prop.getProperty(key).split(","));
                    logger.info("List of classes to be enhanced:" + prop.getProperty(key));
                }
            }
            in.close();
        } catch (Exception e) {
            logger.error("Failed to parse the list of classes to be enhanced.", e);
        }
        return list;
    }

}
